/*
 * ObservableMap.java
 *
 * Created on November 17, 2002, 2:20 AM
 *
 * Copyright 2008 the Desert Labs Project (http://desertlabs.us)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package us.desertlabs.collections;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Observable;
import java.util.Set;

/**
 * This is a <CODE>Map</CODE> implementation that allows observation of
 * changes and important events through the <CODE>Observer</CODE> interface.
 * The <CODE>update</CODE> method on all the <CODE>Observer</CODE>s will be
 * called with "null" as the only parameter regardless of the change that
 * occurs. The <CODE>Observer</CODE> is responsible for examining the <CODE>ObservableMap</CODE>
 * to determine what has changed.
 *
 * @param <K> the datatype of the keys
 * @param <V> the datatype of the values
 * @author Brandon Franklin
 */
public class ObservableMap<K, V> extends Observable implements Map<K, V> {

    /**
     * The <CODE>Map</CODE> implementation that is actually used to store the
     * data
     */
    final private Map<K, V> backingMap;

    /**
     * Creates a new instance of <CODE>ObservableMap</CODE>, backed by a
     * default <CODE>HashMap</CODE>.
     */
    public ObservableMap() {
        this( new HashMap<K, V>() );
    }

    /**
     * Creates a new instance of <CODE>ObservableMap</CODE>, backed by the
     * supplied <CODE>Map</CODE> implementation.
     *
     * @param backingMap the <CODE>Map</CODE> to back this instance with
     */
    public ObservableMap( final Map<K, V> backingMap ) {
        this.backingMap = backingMap;
    }

    /**
     * {@inheritDoc} Calling this method will trigger notification of all of
     * this object's <CODE>Observer</CODE>s.
     */
    @Override
    public void clear() {
        backingMap.clear();

        setChanged();
        notifyObservers();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean containsKey( final Object key ) {
        return backingMap.containsKey( key );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean containsValue( final Object value ) {
        return backingMap.containsValue( value );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Set<Entry<K, V>> entrySet() {
        return backingMap.entrySet();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public V get( final Object key ) {
        return backingMap.get( key );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isEmpty() {
        return backingMap.isEmpty();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Set<K> keySet() {
        return backingMap.keySet();
    }

    /**
     * {@inheritDoc} Calling this method will trigger notification of all of
     * this object's <CODE>Observer</CODE>s.
     */
    @Override
    public V put( final K key, final V value ) {
        final V ret = backingMap.put( key, value );

        setChanged();
        notifyObservers();

        return ret;
    }

    /**
     * {@inheritDoc} Calling this method will trigger notification of all of
     * this object's <CODE>Observer</CODE>s.
     */
    @Override
    public void putAll( final Map<? extends K, ? extends V> m ) {
        backingMap.putAll( m );

        setChanged();
        notifyObservers();
    }

    /**
     * {@inheritDoc} Calling this method will trigger notification of all of
     * this object's <CODE>Observer</CODE>s.
     */
    @Override
    public V remove( final Object key ) {

        final V ret = backingMap.remove( key );

        setChanged();
        notifyObservers();

        return ret;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int size() {
        return backingMap.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Collection<V> values() {
        return backingMap.values();
    }
}
